package com.example.server.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.bean.Employee;
import com.example.server.bean.MailLog;
import com.example.server.dao.EmployeeDao;
import com.example.server.dao.MailLogDao;
import com.example.server.service.IEmployeeService;
import com.example.server.util.MailConstants;
import com.example.server.util.PageRespBean;
import com.example.server.util.RespBean;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 别怿言
 * @since 2023-02-03
 */
@Service
@Transactional
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MailLogDao mailLogDao;


    @Override
    public PageRespBean getEmloyeeByPage(Integer currentPage, Integer size,
                                         Employee employee, LocalDate[] beginDateScope) {
        Page<Employee> page = new Page<>(currentPage,size);
        IPage<Employee> employeeByPage = employeeDao.getEmloyeeByPage(page,employee,beginDateScope);
        PageRespBean pageRespBean = new PageRespBean(employeeByPage.getTotal(), employeeByPage.getRecords());
        return pageRespBean;

    }

    @Override
    public RespBean maxWorkId() {
        Integer workid = employeeDao.maxWorkId()+1;
        String prefix = "00000000";
        String maxWorkid = prefix.substring(workid.toString().length())+workid;
        return RespBean.success("查询成功",maxWorkid);
    }

    @Override
    public RespBean addEmployee(Employee employee) {
        LocalDate begindate = employee.getBeginContract();
        LocalDate enddate = employee.getEndContract();
        Long days = begindate.until(enddate, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.valueOf(decimalFormat.format(days/365.00)));
        if(1==employeeDao.insert(employee)){
            Employee e = employeeDao.getEmployee(employee.getId()).get(0);

            String msgId = UUID.randomUUID().toString();
            MailLog mailLog = new MailLog();
            mailLog.setMsgId(msgId);
            mailLog.setEid(e.getId());
            mailLog.setStatus(MailConstants.DELIVERING);
            mailLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY);
            mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLogDao.insert(mailLog);

            rabbitTemplate.convertAndSend("MailConstants.MAIL_EXCHANGE_NAME",
                    MailConstants.MAIL_ROUTING_KEY,e, new CorrelationData(msgId));
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @Override
    public List<Employee> getEmployee(Integer id) {
        return employeeDao.getEmployee(id);
    }

    @Override
    public PageRespBean getSalariesByPage(Integer currentPage, Integer size) {
        Page<Employee> page = new Page<>(currentPage,size);
        IPage<Employee> employeepage = employeeDao.getEmloyeeWithSalary(page);
        PageRespBean pageRespBean = new PageRespBean(employeepage.getTotal(), employeepage.getRecords());
        return pageRespBean;
    }
}
